package com.lengxf.redis.service;


import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * redis Hash 服务操作类
 *
 * @Author Lengxf
 */
@Component
@AllArgsConstructor
@SuppressWarnings({"rawtypes", "unchecked"})
public class RedisHashService {

    private RedisTemplate redisTemplate;

    public <T> void hashSet(String key, String field, T value) {
        redisTemplate.opsForHash().put(key, field, value);
    }

    public <T> T hashGet(String key, String field) {
        HashOperations<String, String, T> hashOperations = redisTemplate.opsForHash();
        return hashOperations.get(key, field);
    }

    public Set<String> hashKeys(String key) {
        return redisTemplate.opsForHash().keys(key);
    }

    public boolean hashDel(String key, Object[] fields) {
        return redisTemplate.opsForHash().delete(key, fields) > 0;
    }

    public boolean hashHasKey(String key, String field) {
        return redisTemplate.opsForHash().hasKey(key, field);
    }

}
